package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.serverUtils.ExcelUtil;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.model.OperationLog;
import com.ybkj.daijia.server.service.OperationLogService;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "operationlog")
public class OperationLogController {

    @Autowired
    private OperationLogService operationLogService;


    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list(
        @RequestParam(required = false, value = "operating") String operating,
        @RequestParam(required = false, value = "startTime") Date start,
        @RequestParam(required = false, value = "endTime") Date end
    ) throws ParseException {
        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("operationlog")) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("operationlog/list");

            mv.addObject("page", operationLogService.queryAll(operating, start, end));

            mv.addObject("operating", operating);
            mv.addObject("start", start);
            mv.addObject("end", end);

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }


    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public @ResponseBody
    Result<Integer> delete(Long id, HttpServletRequest req) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("operationlog")) {
            Result<Integer> result = new Result<Integer>();

            if (null != id) {
                OperationLog operationLog = operationLogService.findOne(id);
                if (null != operationLog) {
                    operationLogService.delete(operationLog);
                    result.setSuccess(true);
                    result.setMessage("删除成功");
                } else {
                    result.setMessage("没有可以删除的数据");
                }
            } else {
                result.setMessage("没有可以删除的数据");
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "export", method = RequestMethod.GET)
    public @ResponseBody
    String export(
        @RequestParam(required = false, value = "operating") String operating,
        @RequestParam(required = false, value = "startTime") Date start,
        @RequestParam(required = false, value = "endTime") Date end,
        @RequestParam(required = true, value = "timestamp") Long timestamp,
        HttpServletRequest req, HttpServletResponse response,
        HttpSession session) throws IOException, ParseException {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("operationlog")) {

            List<OperationLog> content = operationLogService
                .export(session, operating, start, end, timestamp);

            List<List<String>> rowlist = new ArrayList<List<String>>();
            String[] head = new String[27];
            head[0] = ("操作详细");
            head[1] = ("所属公司");
            head[2] = ("操作人");
            head[3] = ("ip地址");
            head[4] = ("时间");

            SimpleDateFormat format = com.ybkj.daijia.constant.DateConstant.DATE_TIME;
            for (OperationLog operationLog : content) {
                List<String> celllist = new ArrayList<String>();

                celllist.add(operationLog.getDetail());
                celllist.add(operationLog.getCompanyName());
                celllist.add(operationLog.getOperating());
                celllist.add(operationLog.getHost());
                celllist.add(format.format(operationLog.getCreated()));

                rowlist.add(celllist);
            }

            return ExcelUtil.createExcel(req, response, head, rowlist);
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "exportProcess", method = RequestMethod.GET)
    public @ResponseBody
    Object exportProcess(HttpSession session,
        @RequestParam(required = true, value = "timestamp") Long timestamp) {
        return session.getAttribute("exportOperationlogProcess_" + timestamp);
    }

    @org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(WebDataBinder dataBinder) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

    }
}

